package com.niu.shop.service.admin.marketing.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.niu.core.common.domain.PageResult;
import com.niu.core.common.domain.PageParam;
import com.niu.shop.entity.exchange.ShopPointExchange;
import com.niu.shop.mapper.exchange.ShopPointExchangeMapper;
import com.niu.shop.service.admin.marketing.param.ShopPointExchangeParam;
import com.niu.shop.service.admin.marketing.param.ShopPointExchangeSearchParam;
import com.niu.shop.service.admin.marketing.IShopPointExchangeService;
import com.niu.shop.service.admin.marketing.vo.ShopPointExchangeInfoVo;
import com.niu.shop.service.admin.marketing.vo.ShopPointExchangeListVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;


/**
 * 积分兑换实现
 */
@Service
public class ShopPointExchangeServiceImpl implements IShopPointExchangeService {
        
    @Resource
    ShopPointExchangeMapper shopPointExchangeMapper;

    /**
     * 积分兑换列表
     * @param pageParam 分页参数
     * @param searchParam 搜索参数
     * @return PageResult<ShopPointExchangeListVo>
     */
    @Override
    public PageResult<ShopPointExchangeListVo> list(PageParam pageParam, ShopPointExchangeSearchParam searchParam) {
        Integer page  = pageParam.getPage();
        Integer limit =pageParam.getLimit();

        QueryWrapper<ShopPointExchange> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc(Arrays.asList("sort", "total_price_num"));

        IPage<ShopPointExchange> iPage = shopPointExchangeMapper.selectPage(new Page<>(page, limit), queryWrapper);
        List<ShopPointExchangeListVo> list = new LinkedList<>();
        for (ShopPointExchange item : iPage.getRecords()) {
            ShopPointExchangeListVo vo = new ShopPointExchangeListVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }
        return PageResult.build(page, limit, iPage.getTotal()).setData(list);
    }

    /**
     * 积分兑换详情
     * @param id 主键
     * @return ShopPointExchangeInfoVo
     */
    @Override
    public ShopPointExchangeInfoVo info(Integer id) {
        ShopPointExchange model = shopPointExchangeMapper.selectOne(
                new QueryWrapper<ShopPointExchange>()
                    .eq("total_price_num", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        ShopPointExchangeInfoVo vo = new ShopPointExchangeInfoVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 积分兑换添加
     * @param addParam 参数
     */
    @Override
    public void add(ShopPointExchangeParam addParam) {
        ShopPointExchange model = new ShopPointExchange();
        model.setSiteId(addParam.getSiteId());
        model.setType(addParam.getType());
        model.setNames(addParam.getNames());
        model.setTitle(addParam.getTitle());
        //model.setImage(UrlUtils.toRelativeUrl(addParam.getImage()));
        model.setStatus(addParam.getStatus());
        model.setProductDetail(addParam.getProductDetail());
        model.setPoint(addParam.getPoint());
        model.setPrice(addParam.getPrice());
        model.setLimitNum(addParam.getLimitNum());
        model.setContent(addParam.getContent());
        model.setSort(addParam.getSort());
        model.setTotalPointNum(addParam.getTotalPointNum());
        model.setTotalOrderNum(addParam.getTotalOrderNum());
        model.setTotalMemberNum(addParam.getTotalMemberNum());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setStock(addParam.getStock());
        model.setTotalExchangeNum(addParam.getTotalExchangeNum());
        shopPointExchangeMapper.insert(model);
    }

    /**
     * 积分兑换编辑
     *
     * @param editParam 参数
     */
    @Override
    public void edit(Integer id, ShopPointExchangeParam editParam) {
        ShopPointExchange model = shopPointExchangeMapper.selectOne(
                new QueryWrapper<ShopPointExchange>()
                    .eq("total_price_num",  id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");
        //model.setTotalPriceNum(id);
        
        model.setSiteId(editParam.getSiteId());
        model.setType(editParam.getType());
        model.setNames(editParam.getNames());
        model.setTitle(editParam.getTitle());
        //model.setImage(UrlUtils.toRelativeUrl(editParam.getImage()));
        model.setStatus(editParam.getStatus());
        model.setProductDetail(editParam.getProductDetail());
        model.setPoint(editParam.getPoint());
        model.setPrice(editParam.getPrice());
        model.setLimitNum(editParam.getLimitNum());
        model.setContent(editParam.getContent());
        model.setSort(editParam.getSort());
        model.setTotalPointNum(editParam.getTotalPointNum());
        model.setTotalPriceNum(editParam.getTotalPriceNum());
        model.setTotalOrderNum(editParam.getTotalOrderNum());
        model.setTotalMemberNum(editParam.getTotalMemberNum());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        model.setStock(editParam.getStock());
        model.setTotalExchangeNum(editParam.getTotalExchangeNum());
        shopPointExchangeMapper.updateById(model);
    }

    /**
     * 积分兑换删除
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        ShopPointExchange model = shopPointExchangeMapper.selectOne(
                new QueryWrapper<ShopPointExchange>()
                    .eq("total_price_num", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        shopPointExchangeMapper.delete(new QueryWrapper<ShopPointExchange>().eq("total_price_num", id));
    }

}
